package light.head.controller.el;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.el.TrainedReportDAO;
import light.head.dto.TrainedReportDTO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.model.el.TrainedReport;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 受训报告
 */
@Controller
@RequestMapping(Url.C_TRAINEDREPORT)
public class TrainedReportController extends SupporterController{
	private TrainedReportDAO trDAO;
	
	@RequestMapping(Url.REPORTSTUDENT)
	public ModelAndView reportStudent(HttpServletRequest request, HttpServletResponse response){
		String clc = request.getParameter(Parm.CLC);
		request.setAttribute(Parm.CLC, clc);
		List<TrainedReport> list1 = trDAO.findByClcAndUc(Integer.parseInt(clc),cu_code(request));
		if(list1 != null && list1.size() > 0){
			request.setAttribute(Parm.LIST_1, list1);
		}
		return new ModelAndView(Url.TRAINEDREPORT_STUDENTREPORT);
	}
	@RequestMapping(Url.REPORTTEACHER)
	public ModelAndView reportTeacher(HttpServletRequest request, HttpServletResponse response){
		String clc = request.getParameter(Parm.CLC);
		if(clc != null){
			List<String> list = trDAO.findPercentByClc(Integer.parseInt(clc));
			if(list != null && list.size() > 0){
				request.setAttribute(Parm.LIST, list);
			}
			List<TrainedReport> list1 = trDAO.findByClcAndTrc5(Integer.parseInt(clc));
			if(list1 != null && list1.size() > 0){
				request.setAttribute(Parm.LIST_1, list1);
			}
		}
		return new ModelAndView(Url.TRAINEDREPORT_TEACHERREPORT);
	}
	
	@RequestMapping(Url.SAVE)
	@Transactional(rollbackFor = Exception.class)
	public void save(TrainedReportDTO tr,HttpServletRequest request, HttpServletResponse response){
		String clc = request.getParameter(Parm.CLC);
		RESULT result = RESULT.ERROR;
		if(tr.getTrList() != null && tr.getTrList().size() > 0 && isNotNull(clc)){
			Integer cCode = Integer.parseInt(clc);
			Integer uc = cu_code(request);
			Integer oc = cu_organCode(request);
			Date time = new Date();
			for(TrainedReport temp : tr.getTrList()){
				temp.setClc(cCode);
				temp.setCt(time);
				temp.setDeleted(false);
				temp.setOc(oc);
				temp.setUc(uc);
				trDAO.add(temp);
			}
			result = RESULT.SUCCESS;
		}
		if (result == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
	
	@Autowired
	public void setTrDAO(TrainedReportDAO trDAO) {
		this.trDAO = trDAO;
	}
}
